package com.feonix.common.view.tree;

import com.feonix.common.BaseInfo;
import com.feonix.common.permission.PermissionStructure;
import com.feonix.common.util.StringUtil;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class TreeNode extends PermissionStructure implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 当 setting.checkable = true 时有效，设定节点的 CheckBox 是否被勾选 如果不想使用 checked
	 * 属性，可以自定义记录勾选状态的字段，请修改 checkedCol 默认值：false
	 */
	private Boolean checked = Boolean.valueOf(false);

	/**
	 * 设定节点在鼠标点击后做的事情，相当于 onclick="...."
	 */
	private String click;

	/**
	 * 设定节点的自定义图标，以替换 css 样式中配置的普通图标。
	 */
	private String icon;

	/**
	 * 与 icon 功能类似，对于父节点允许通过同时设定 iconOpen 和 iconClose 设定展开、 折叠时的自定义图标，以替换 css
	 * 样式中配置的普通图标。
	 */
	private String iconClose;

	/**
	 * 与 icon 功能类似，对于父节点允许通过同时设定 iconOpen 和 iconClose 设定展开、折叠时的自定义图标， 以替换 css
	 * 样式中配置的普通图标。
	 */
	private String iconOpen;

	/**
	 * 设定节点的自定义图标在 CSS 中对应的自定义 ClassName
	 */
	private String iconSkin;

	/**
	 * 设置某节点是否为父节点。
	 */
	private Boolean isParent = Boolean.valueOf(false);

	/**
	 * 节点显示的名称。
	 */
	private String name;

	/**
	 * 当 checkable 为 true 时，设置该节点是否显示 checkbox 或 radio 注意： 1、此功能仅仅影响 checkbox 或
	 * radio 的显示与否，不影响 zTree 内部关于checked等属性的计算。
	 */
	private Boolean nocheck = Boolean.valueOf(false);

	/**
	 * 1、设置节点的 checkbox / radio 是否禁用 [setting.check.enable = true 时有效]
	 * 2、为了解决部分朋友生成 json 数据出现的兼容问题, 支持 "false","true" 字符串格式的数据
	 * 3、请勿对已加载的节点修改此属性，禁止 或 取消禁止 请使用 setChkDisabled() 方法 4、初始化时，如果需要子孙节点继承父节点的
	 * chkDisabled 属性，请设置 setting.check.chkDisabledInherit 属性
	 */
	private boolean chkDisabled = false;

	/**
	 * 设置父节点初始化展开状态。
	 */
	private Boolean open = Boolean.valueOf(true);

	/**
	 * 对于存在 url 属性的节点，设置点击后跳转的目标，同超链接的 target 属性（"_blank", "_self"等）
	 */
	private String target = "page";

	/**
	 * 指定节点被点击后的跳转页面 URL 地址 过于复杂的操作，建议通过 click 事件进行控制处理
	 */
	private String url;

	/**
	 * 节点id
	 */
	private String id;

	/**
	 * 父节点
	 */
	private String pId = "root";

	/**
	 * 版选状态
	 */
	private boolean halfCheck = false;

	/**
	 * 是否禁用
	 */
	private boolean isDisabled = false;

	/**
	 * url的参数
	 */
	private StringBuffer params = new StringBuffer();

	/**
	 * 附件参数
	 */
	private Map<String, Object> attr = new HashMap<String, Object>();

	/**
	 * 字体
	 */
	private Map<String, String> font = new LinkedHashMap<String, String>();

	public static TreeNode getInstance() {
		return new TreeNode();
	}

	public static TreeNode getInstance(String id, String pid, String name) {
		TreeNode treeNode = getInstance().setId(id).setpId(pid).setName(name);
		return treeNode;
	}

	public static TreeNode getInstance(String id, String pid, String name,
			boolean hasChild) {
		TreeNode treeNode = getInstance().setId(id).setpId(pid).setName(name)
				.setIsParent(Boolean.valueOf(hasChild));
		return treeNode;
	}

	public static TreeNode getInstance(String id, String pid, String name,
			boolean hasChild, boolean isOpen) {
		TreeNode treeNode = getInstance().setId(id).setpId(pid).setName(name)
				.setIsParent(Boolean.valueOf(hasChild)).setOpen(Boolean.valueOf(isOpen));
		return treeNode;
	}

	public static TreeNode getInstance(String id, String pid, String name,
			String url) {
		TreeNode treeNode = getInstance(id, pid, name).setUrl(url);
		return treeNode;
	}

	public static TreeNode getInstance(String id, String pid, String name,
			String url, boolean hasChild) {
		TreeNode treeNode = getInstance(id, pid, name, url)
				.setIsParent(Boolean.valueOf(hasChild));
		return treeNode;
	}

	public static TreeNode getInstance(String id, String pid, String name,
			String url, boolean hasChild, boolean isOpen) {
		TreeNode treeNode = getInstance(id, pid, name, url, hasChild)
				.setOpen(Boolean.valueOf(isOpen));
		return treeNode;
	}

	public Boolean getChecked() {
		return this.checked;
	}

	public TreeNode setChecked(Boolean checked) {
		this.checked = checked;
		return this;
	}

	public String getClick() {
		return this.click;
	}

	public TreeNode setClick(String click) {
		this.click = click;
		return this;
	}

	public String getIcon() {
		return this.icon;
	}

	public TreeNode setIcon(String icon) {
		this.icon = icon;
		return this;
	}

	public String getIconClose() {
		return this.iconClose;
	}

	public TreeNode setIconClose(String iconClose) {
		this.iconClose = iconClose;
		return this;
	}

	public String getIconOpen() {
		return this.iconOpen;
	}

	public TreeNode setIconOpen(String iconOpen) {
		this.iconOpen = iconOpen;
		return this;
	}

	public String getIconSkin() {
		return this.iconSkin;
	}

	public TreeNode setIconSkin(String iconSkin) {
		this.iconSkin = iconSkin;
		return this;
	}

	public Boolean getIsParent() {
		return this.isParent;
	}

	public TreeNode setIsParent(Boolean isParent) {
		this.isParent = isParent;
		return this;
	}

	public String getName() {
		return this.name;
	}

	public TreeNode setName(String name) {
		this.name = name;
		return this;
	}

	public Boolean getNocheck() {
		return this.nocheck;
	}

	public TreeNode setNocheck(Boolean nocheck) {
		this.nocheck = nocheck;
		return this;
	}

	public Boolean getOpen() {
		return this.open;
	}

	public TreeNode setOpen(Boolean open) {
		this.open = open;
		return this;
	}

	public String getTarget() {
		return this.target;
	}

	public TreeNode setTarget(String target) {
		this.target = target;
		return this;
	}

	public String getUrl() {
		if ((StringUtil.isNotEmpty(this.url)) && (!this.url.toLowerCase().startsWith("http://"))
				&& (!this.url.toLowerCase().startsWith("https://"))) {
			if (this.url.contains("?")) {
				this.url += "&";
			} else {
				this.url += "?";
			}
			if (this.params.length() > 0) {
				this.url += this.params.deleteCharAt(this.params.length() - 1);
			}
		}
		StringUtil.cleanStringBuffer(new StringBuffer[] { this.params });
		return this.url;
	}

	public TreeNode setUrl(String url) {
		if (StringUtil.isEmpty(url)) {
			return this;
		}
		if ((url.toLowerCase().startsWith("http://")) || (url.toLowerCase().startsWith("https://"))) {
			this.url = url;
		} else if (url.startsWith("/")) {
			url = BaseInfo.getContextPath() + url;
		} else {
			url = BaseInfo.getContextPath() + "/" + url;
		}
		this.url = url;
		return this;
	}

	public String getId() {
		return this.id;
	}

	public TreeNode setId(String id) {
		this.id = id;
		return this;
	}

	public boolean isHalfCheck() {
		return this.halfCheck;
	}

	public TreeNode setHalfCheck(boolean halfCheck) {
		this.halfCheck = halfCheck;
		return this;
	}

	public boolean isChkDisabled() {
		return this.chkDisabled;
	}

	public TreeNode setChkDisabled(boolean chkDisabled) {
		this.chkDisabled = chkDisabled;
		return this;
	}

	public String getpId() {
		return this.pId;
	}

	public TreeNode setpId(String pId) {
		this.pId = pId;
		return this;
	}

	public boolean getIsDisabled() {
		return this.isDisabled;
	}

	public TreeNode setIsDisabled(boolean isDisabled) {
		this.isDisabled = isDisabled;
		if (isDisabled) {
			addFont("color", "silver");
		}
		return this;
	}

	public Map<String, String> getFont() {
		return this.font;
	}

	public TreeNode addFont(String key, String value) {
		this.font.put(key, value);
		return this;
	}

	public TreeNode addAttr(String key, Object value) {
		if ((value != null) && (StringUtil.isNotEmpty(key))) {
			this.attr.put(key, value);
		}
		return this;
	}

	public TreeNode addParam(String key, Object value) {
		String str = StringUtil.getString(value);
		if ((StringUtil.isEmpty(key)) || (StringUtil.isEmpty(str))) {
			return this;
		}
		this.params.append(key).append("=").append(StringUtil.encoder(str)).append("&");
		return this;
	}

	public TreeNode setPermission(String moduleId, String functionId) {
		setModuleId(moduleId);
		setFunctionId(functionId);
		return this;
	}

	public TreeNode setPermission(String moduleId, String functionId, String params) {
		setModuleId(moduleId);
		setFunctionId(functionId);
		setParams(params);
		return this;
	}

	public TreeNode setAllowed(boolean allowedAdmin, boolean allowedGroup) {
		setAllowedAdmin(Boolean.valueOf(allowedAdmin));
		setAllowedGroup(Boolean.valueOf(allowedGroup));
		return this;
	}

	public Map<String, Object> getAttr() {
		return this.attr;
	}
}
